﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using FantasyDraft2.Models;

namespace FantasyDraft2.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        FantasyDraftEntities _fantasyDb = new FantasyDraftEntities();
        
        public ActionResult Index()
        {
            var users = _fantasyDb.Users.ToList();
            return View(users);
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            //players assigned to id to the veiw
            var player = _fantasyDb.Players.Where(p => p.TeamId == id).ToList();
            return View(player);
        }

        
         //GET: /User/Register

        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /User/Register

        [HttpPost]
        public ActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                _fantasyDb.Users.Add(user);
                _fantasyDb.SaveChanges();

                string name = user.FirstName;

                Session.Add("User", name);
                
                return RedirectToAction("Index");
            }
            return View(user);
        }

        //
        // GET: /User/Logon

        public ActionResult Logon()
        {
            return View();
        }

        //
        // POST: /User/Logon
        [HttpPost]
        public ActionResult Logon(User user)
        {

            FormsAuthentication.SetAuthCookie(user.UserName, true);
            return RedirectToAction("Index", "User");
        }

        // GET: /Account/Logoff
        public ActionResult Logoff()
        {
            return RedirectToAction("Index", "Home");
        }

    }
}
